<script setup lang="ts" name="PanelExternalErrors">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import { getErrors, addError, editError, removeError } from '../../utils/externalErrors'
import { isExternalErrorsSupported } from '../../utils/bpmnPropertyUtils/eventDefinition'
import EventEmitter from '@/utils/EventEmitter'
import useMessageBox from '@/hooks/useMessageBox'
import ErrorDrawer from './components/ErrorDrawer.vue'

const { messageBox } = useMessageBox()

const ErrorDrawerRef = ref(null)

const errorRaw = ref([])
const errorList = ref([])

const editIndex = ref(-1)

const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const add = () => {
  editIndex.value = -1
  ErrorDrawerRef.value.showDrawer()
}

const edit = (row, idx) => {
  editIndex.value = idx
  ErrorDrawerRef.value.showDrawer(row)
}

const deleteRow = (index) => {
  const msg = '确认<span>删除</span>该错误状态吗？'
  const msgTips = ''
  messageBox('danger', true, msg, msgTips, () => {
    removeError(mapStates.value.getActive(), errorRaw.value[index])
  })
}

const errorChange = (v) => {
  if (editIndex.value === -1) {
    addError(mapStates.value.getActive(), v)
  } else {
    editError(mapStates.value.getActive(), errorRaw.value[editIndex.value], v)
  }
}

const initData = () => {
  errorRaw.value = markRaw(getErrors(mapStates.value.getActive()))
  console.log(errorRaw.value, '错误列表数据')
  errorList.value = []
  errorRaw.value.forEach((item) => {
    errorList.value.push({
      id: item.errorRef.id,
      name: item.errorRef.name,
      errorCode: item.errorRef.errorCode,
      errorMessage: item.errorRef.errorMessage,
      expression: item.expression
    })
  })
}

const eventEmitterListener = () => {
  if (isExternalErrorsSupported(mapStates.value.getActive())) {
    initData()
  }
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  initData()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="panel-external-errors">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:bug-2-fill"></svg-icon>
        <h2>错误</h2>
        <el-button type="primary" size="small" plain circle>{{ errorList.length }}</el-button>
      </div>
    </template>
    <el-form-item>
      <el-table :data="errorList" border>
        <el-table-column type="index" label="序号" width="60" fixed="left" />
        <el-table-column prop="name" label="名称" show-overflow-tooltip />
        <el-table-column prop="errorCode" label="错误码" show-overflow-tooltip />
        <el-table-column prop="errorMessage" label="错误信息" show-overflow-tooltip min-width="90" />
        <el-table-column prop="expression" label="表达式" show-overflow-tooltip />
        <el-table-column prop="operate" label="操作" width="110" align="center" fixed="right">
          <template #default="scope">
            <el-button type="primary" icon="Edit" circle @click="edit(scope.row, scope.$index)"></el-button>
            <el-button type="danger" icon="Delete" circle @click="deleteRow(scope.$index)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button class="add" type="primary" icon="Plus" @click="add">添加错误状态</el-button>
    </el-form-item>
    <ErrorDrawer ref="ErrorDrawerRef" @save="errorChange"></ErrorDrawer>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        display: flex;
        flex: 1;
        justify-content: flex-start;
        font-weight: bold;
        font-size: 14px;
      }

      .el-button {
        margin-left: 12px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}

.el-form-item {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;

    .el-button.add {
      width: 100%;
      margin: 12px 0 0;
    }
  }
}
</style>
